package com.yllt4cloud.common.ddbase.constant;


import com.yllt4cloud.common.ddbase.constant.entity.FileEntity;
import com.yllt4cloud.common.ddbase.constant.entity.InvocationContext;
import com.yllt4cloud.common.ddbase.constant.entity.SessionEntity;
import com.yllt4cloud.common.ddbase.constant.entity.ThreadLocalManager;
import com.yllt4cloud.common.ddbase.param.BaseParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Administrator
 */
public class ApplicationContext {


    private ApplicationContext() {
    }

    private final Logger logger = LoggerFactory
            .getLogger(ApplicationContext.class);
    public static ApplicationContext CTX = new ApplicationContext();
    private ThreadLocalManager<InvocationContext> threadLocalManager = new ThreadLocalManager<InvocationContext>();

    public ThreadLocalManager<InvocationContext> getThreadLocalManager() {
        return threadLocalManager;
    }

    public InvocationContext getInvocationContext() {
        return getThreadLocalManager().getValue();
    }

    public SessionEntity getSessionEntity() {
        return null != getThreadLocalManager().getValue() ? getThreadLocalManager().getValue().getSessionEntity() : null;
    }

    //修改 取员工id
    public Integer currentClerkId() {
        return getSessionEntity().getUserInfo().getClerkId();
    }
   //取登入用户id
    public Integer currentUserId() {
        return getSessionEntity().getUserInfo().getUserId();
    }

    //取登入的管理员用户名
    public String currentUserName() {
        return getSessionEntity().getUserInfo().getUserName();
    }

    //放置用户session对象
    public void setSessionEntity(SessionEntity sessionEntity) {
        getThreadLocalManager().getValue().setSessionEntity(sessionEntity);
    }

    //放置参数对象
    public void setBaseParam(BaseParam param) {
        getThreadLocalManager().getValue().setBaseParam(param);
    }

    //取出参数对象
    public BaseParam getBaseParam() {
        return getThreadLocalManager().getValue().getBaseParam();
    }

    public List<FileEntity> getFileList() {
        return getThreadLocalManager().getValue().getFileList();
    }

    //放置文件
    public void setFileList(List<FileEntity> fileList) {
        getThreadLocalManager().getValue().setFileList(fileList);
    }

    public HttpServletRequest getRequest() {
        return getThreadLocalManager().getValue().getRequest();
    }

    //放置InvocationContext
    public void openThreadLocal(InvocationContext invocationContext) {
        getThreadLocalManager().openThreadLocal(invocationContext);
        logger.debug("openThreadLocal");
    }

    //关闭
    public void closeThreadLocal() {
        getThreadLocalManager().closeThreadLocal();
    }

    //放置request
    public void setRequest(HttpServletRequest request) {
        getThreadLocalManager().getValue().setRequest(request);
    }
}
